package hong.jun.pet_home.service.pay.impl;

import hong.jun.basic.base.service.impl.BaseServiceImpl;
import hong.jun.basic.util.AlipayUtils;
import hong.jun.pet_home.domain.order.ProductOrder;
import hong.jun.pet_home.domain.pay.AlipayInfo;
import hong.jun.pet_home.domain.pay.PayBill;
import hong.jun.pet_home.mapper.AlipayInfoMapper;
import hong.jun.pet_home.mapper.PayBillMapper;
import hong.jun.pet_home.mapper.ProductOrderMapper;
import hong.jun.pet_home.service.pay.IPayBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {
    @Autowired
    private AlipayInfoMapper alipayInfoMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private ProductOrderMapper productOrderMapper;

    /**
     * 前台支付跳转
     */
    @Override
    public String pay(PayBill payBill) {
        //判断支付方式
        int payChannel = payBill.getPayChannel().intValue();//long转int
        switch (payChannel) {
            case 0:
                System.out.println("余额支付");
            case 1:
                System.out.println("支付宝支付");
                //查找该商户的支付宝账号和密钥。
                AlipayInfo alipayInfo = alipayInfoMapper.loadByShopId(payBill.getShop_id());
                //System.out.println("--------------"+alipayInfo);
                return AlipayUtils.sendPayRequest(alipayInfo, payBill);
            case 2:
                System.out.println("微信支付");
            case 3:
                System.out.println("银联支付");

        }
        return null;
    }

    /**
     * 根据统一支付单号查找支付单
     */
    @Override
    public PayBill getByUnionPayBillSn(String unionPaySn) {
        PayBill payBill = payBillMapper.getByUnionPayBillSn(unionPaySn);
        return payBill;
    }

    /**
     * 改变支付状态
     * 参数 服务类型 商品id
     */
    @Override
    public void changeOrderState(String businessType, Long businessKey, PayBill payBill) {
        changepaybaill(payBill);//改变支付单状态
        switch (businessType) {
            case "businesstype_product"://宠物保养购买服务
                System.out.println("宠物保养购买服务");
                //改变订单状态

            case "businesstype_recharge"://充值余额
                System.out.println("充值余额");
                //改变订单状态

            case "businessType_adopt"://宠物收购
                System.out.println("宠物收购");
                //改变订单状态

            case "businesstype_find_master"://收养宠物
                System.out.println("收养宠物");
                //改变订单状态

            case "businesstype_goods"://购买商品
                System.out.println("购买商品");
                //改变订单状态

        }
    }


    /**
     * 改变支付单状态
     */
    public void changepaybaill(PayBill payBill) {
        payBill.setState(1);//支付状态 0待支付 1已支付 -1取消
        payBillMapper.update(payBill);
    }

    /**
     * 改变服务订单状态
     */
    public void changeorder(Long businessKey) {


    }
}
